package com.quanyue.haircut.shop.controller;

import com.quanyue.auto.util.ServiceResult;
import com.quanyue.auto.web.Principal;
import com.quanyue.auto.web.Scope;
import com.quanyue.haircut.domain.constant.UserRole;
import com.quanyue.haircut.domain.entity.Coupon;
import com.quanyue.haircut.domain.entity.UserCoupon;
import com.quanyue.haircut.shop.dto.CouponDetail;
import com.quanyue.haircut.shop.service.CouponService;
import com.quanyue.haircut.shop.service.CouponUserService;
import com.vhuomai.mybatis.mapper.page.Page;
import com.vhuomai.mybatis.mapper.page.PageRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Api(tags = "优惠券")
@Scope(role = UserRole.MANAGER)
@RestController
@RequestMapping("/api")
public class CouponController {
    @Autowired
    private CouponService couponService;
    @Autowired
    private CouponUserService couponUserService;

    @ApiOperation(value = "添加优惠券")
    @PostMapping(value = "/coupon", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Coupon> createCoupon(@RequestBody Coupon coupon,
                                               @ApiIgnore Principal principal) {
        coupon.setMerchantNo(principal.getMerchantNo());
        return ServiceResult.success(couponService.createCoupon(coupon));
    }

    @ApiOperation(value = "删除优惠券")
    @DeleteMapping(value = "/coupon", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> deleteCoupon(@RequestParam Integer couponId) {
        couponService.deleteCoupon(couponId);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "优惠券列表")
    @PostMapping(value = "/coupon/page", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Page <Coupon>> couponList(@RequestBody PageRequest request,
                                                    @ApiIgnore Principal principal) {
        return ServiceResult.success(couponService.couponList(request, principal.getMerchantNo()));
    }

    @ApiOperation(value = "添加用户优惠券")
    @PostMapping(value = "/coupon/user", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> createUserCoupon(@RequestBody List <UserCoupon> userCoupons) {
        couponUserService.createCouponUser(userCoupons);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "删除用户优惠券")
    @DeleteMapping(value = "/coupon/user", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> deleteUserCoupon(@RequestParam Integer couponUserId) {
        couponUserService.deleteCouponUser(couponUserId);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "用户优惠券列表")
    @Scope(role = UserRole.USER)
    @PostMapping(value = "/coupon/user/page", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Page <CouponDetail>> couponUserList(@RequestBody PageRequest request,
                                                              @RequestParam Integer bit,
                                                              @ApiIgnore Principal principal) {
        return ServiceResult.success(couponUserService.couponUser(request, bit, principal.getId()));
    }

}
